package com.ssm.circle.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.ssm.circle.api.common.PageInfo;
import com.ssm.circle.api.common.PageResult;
import com.ssm.circle.api.enums.IsDeletedFlagEnum;
import com.ssm.circle.api.req.GetShareMomentReq;
import com.ssm.circle.api.req.RemoveShareMomentReq;
import com.ssm.circle.api.req.SaveMomentCircleReq;
import com.ssm.circle.api.vo.ShareMomentVO;
import com.ssm.circle.server.dao.ShareMomentDao;
import com.ssm.circle.server.entity.dto.UserInfo;
import com.ssm.circle.server.entity.po.ShareMoment;
import com.ssm.circle.server.rpc.UserRpc;
import com.ssm.circle.server.service.ShareMomentService;
import com.ssm.circle.server.utils.LoginUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Log4j2
public class ShareMomentServiceImpl implements ShareMomentService {
    @Resource
    private ShareMomentDao shareMomentDao;
    @Resource
    private UserRpc userRpc;

    @Override
    public Boolean saveMoment(SaveMomentCircleReq req) {
        ShareMoment moment = new ShareMoment();
        moment.setCircleId(req.getCircleId());
        moment.setContent(req.getContent());
        moment.setPicUrls(JSON.toJSONString(req.getPicUrlList()));
        moment.setReplyCount(0);
        moment.setCreatedBy(LoginUtil.getLoginId());
        moment.setCreatedTime(new Date());
        moment.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        return shareMomentDao.save(moment);
    }

    @Override
    public Boolean removeMoment(RemoveShareMomentReq req) {
        return shareMomentDao.delete(req.getId());
    }

    @Override
    public PageResult<ShareMomentVO> getMoments(GetShareMomentReq req) {
        PageInfo pageInfo = req.getPageInfo();
        PageResult<ShareMomentVO> result = new PageResult<>();
        result.setPageNo(pageInfo.getPageNo());
        result.setPageSize(pageInfo.getPageSize());
        int start = (pageInfo.getPageNo() - 1) * pageInfo.getPageSize();
        List<ShareMoment> shareMomentList = shareMomentDao.selectByPage(start, pageInfo.getPageSize());
        if(CollectionUtils.isEmpty(shareMomentList)) {
            return result;
        }

        List<String> userNameList = shareMomentList.stream().map(ShareMoment::getCreatedBy).distinct().collect(Collectors.toList());
        Map<String, UserInfo> userInfoMap = userRpc.batchGetUserInfo(userNameList);
        UserInfo defaultUser = new UserInfo();

        List<ShareMomentVO> collect = shareMomentList.stream().map(shareMoment -> {
            ShareMomentVO shareMomentVO = new ShareMomentVO();
            shareMomentVO.setId(shareMoment.getId());
            shareMomentVO.setCircleId(shareMoment.getCircleId());
            shareMomentVO.setContent(shareMoment.getContent());
            shareMomentVO.setPicUrlList(JSON.parseArray(shareMoment.getPicUrls(), String.class));
            shareMomentVO.setReplyCount(shareMoment.getReplyCount());
            shareMomentVO.setCreatedTime(shareMoment.getCreatedTime().getTime());
            //map中存在该键则返回，不存在则返回默认defaultUser
            UserInfo user = userInfoMap.getOrDefault(shareMoment.getCreatedBy(), defaultUser);
            shareMomentVO.setUserName(user.getNickName());
            shareMomentVO.setUserAvatar(user.getAvatar());
            return shareMomentVO;
        }).collect(Collectors.toList());
        result.setRecords(collect);
        result.setTotal(collect.size());
        return result;
    }

    @Override
    public ShareMoment getByMonentId(Long momentId) {
        return shareMomentDao.queryById(momentId);
    }
}
